package events;

import java.util.EventListener;
import java.util.EventObject;
import java.util.Iterator;

import javax.swing.event.EventListenerList;
import packets.OSPFAcknowledgement;
import packets.OSPFDescription;
import packets.OSPFHello;
import packets.OSPFRequest;
import packets.OSPFUpdate;
import router.NeighborDataStructure;
import router.interf.Interface;

public class EventManager {
	
	private static EventManager instance = null;
	private EventListenerList listeners = new EventListenerList();
	
	private EventManager(){
		
		//Listener die auf eingehende Pakete reagieren
		listeners.add(ReceiveHelloListener.class, new ReceiveHelloListener());
		listeners.add(ReceiveAcknowledgementListener.class, new ReceiveAcknowledgementListener());
		listeners.add(ReceiveDescriptionsListener.class, new ReceiveDescriptionsListener());
		listeners.add(ReceiveRequestListener.class, new ReceiveRequestListener());
		listeners.add(ReceiveUpdateListener.class, new ReceiveUpdateListener());
		
		//Listener die auf abgelaufene Timer reagieren
		listeners.add(InactivityTimerListener.class, new InactivityTimerListener());
		listeners.add(HelloIntervalListener.class, new HelloIntervalListener());
		
	}
	
	public static EventManager getInstance(){
		if(instance == null){
			instance = new EventManager();
			
		}
		
		return instance;
	}
	
	
	
	public synchronized void notifyPacketReceived(ReceivedEvent event){
		
		if(event.getPacket() instanceof OSPFHello){
			for(EventListener l : listeners.getListeners(ReceiveHelloListener.class)){
				((ReceivePacketListener)l).processPacket(event);
			}
		}
		
		else if(event.getPacket() instanceof OSPFAcknowledgement){
			for(EventListener l: listeners.getListeners(ReceiveRequestListener.class)){
				((ReceivePacketListener)l).processPacket(event);
			}
		}
		
		else if(event.getPacket() instanceof OSPFAcknowledgement){
			for(EventListener l: listeners.getListeners(ReceiveAcknowledgementListener.class)){
				((ReceivePacketListener)l).processPacket(event);
			}
		}
		
		else if(event.getPacket() instanceof OSPFDescription){
			for(EventListener l: listeners.getListeners(ReceiveDescriptionsListener.class)){
				((ReceivePacketListener)l).processPacket(event);
			}
		}
		
		else if(event.getPacket() instanceof OSPFRequest){
			for(EventListener l: listeners.getListeners(ReceiveRequestListener.class)){
				((ReceivePacketListener)l).processPacket(event);
			}
		}
		
		else if(event.getPacket() instanceof OSPFUpdate){
			for(EventListener l: listeners.getListeners(ReceiveUpdateListener.class)){
				((ReceivePacketListener)l).processPacket(event);
			}
		}
		
		else System.out.println("Unbekanntes PacketReceived Event!");
	}
	
	public synchronized void notifyTimerExpired(EventObject event){
		
		if(event instanceof RouterDeadEvent){
			for(EventListener l: listeners.getListeners(InactivityTimerListener.class)){
				((InactivityTimerListener)l).processTimerExpired((NeighborDataStructure) event.getSource());
			}
			
		}
		
		else if(event instanceof SendHelloEvent){
			for(EventListener l: listeners.getListeners(HelloIntervalListener.class)){
				((HelloIntervalListener)l).processTimerExpired((Interface)event.getSource());
			}
		}
		
		else if(event instanceof WaitTimerExpiredEvent){
			for(EventListener l: listeners.getListeners(WaitTimerExpiredListener.class)){
				((WaitTimerExpiredListener)l).processTimerExpired((Interface)event.getSource());
			}
		}
		
	}
	

	
	
	
	

}
